#include <iostream>
#include <set>
#include <map>
#include "ejudge/server.h"
#include "mn.h"
#include "start_sym.h"
#include "start.h"
#include <sys/vfs.h>
#include <sys/stat.h>
#include "help.h"
//#include "contests"
using namespace std;
set <string> cmds;
map <string, int> m_cmds;
int status = _Start; //0 -Start  1-contests 2- contest 3-kscontest-runs 4-contest-task  5-contest-task-compiler|runner

void Connect(string &ejdir, string &cntdir)
{
    if (access(ejdir.c_str(), 00) == -1)
    {
        cout << "Error Cannt connected ejudge binary dir corrupted" << endl;
        status = 0;
        return;
    }
    if (access(cntdir.c_str(), 00) == -1)
    {
        cout << "Error Cannt connect ejudge contest dir corrupted" << endl;
        status = 0;
        return;
    }
    cerr << "Goto mg_ejudge" << endl;
    status = _Ejudge;
   mg_ejudge(ejdir, cntdir, cmds, m_cmds);
}

void init()
{
    init_cmd(cmds, m_cmds);
}


int main()
{
    string command;
    init();
    bool Exit = false;
    cout << "Etool " + version << endl;
    do
    {
        cout << symbol;
        cin >> command;
        if (cmds.find(command) == cmds.end())
        {
            cout << "Unknown command "+ command << endl;
            print_help(cmds);
            continue;
        }
        switch  (m_cmds[command])
        {
            case e_connect:
                {string  bin_dir, us_dir; cin >>bin_dir >> us_dir;
                 Connect(bin_dir, us_dir);} break;
            case e_exit: Exit = true; break;
            case e_help: print_help(cmds); break;
            default:
                print_help(cmds); break;
        }
    }
    while (!Exit);
    cout << "Good Bye" << endl;
}
